rm(list = ls())
library(dplyr)
library(randomForest)
library(spduration)


## Load the full dataset
load("Data/data_full.RData")

##############################################################################
## Note: These models may take several days to run
##############################################################################

##############################################################################
###### Estimating SPDM model with full data
##############################################################################
data_full <- as.data.frame(data_full)
basket_spdm <- spdur(
  duration ~ verbCoop + verbConf +  matlCoop + matlConf +
            verbCoop_cum + matlCoop_cum + verbConf_cum +  matlConf_cum +
            ratio_GDP + delta_ratio_GDP + 
            idealpointdistance + delta_idealpointdistance +
            miliexp_ratio +  delta_miliexp_ratio +  milperson_ratio + delta_milperson_ratio +
            sfi_high+ sfi_low + 
            high_GDPPC_log + llow_GDPPC_log +  
            regime_dur_max + regime_dur_min +
            low_democracy + high_democracy + delta_low_democracy + delta_high_democracy + 
            lnminidist,
  atrisk ~  verbCoop + verbConf +  matlCoop + matlConf +
          verbCoop_cum + matlCoop_cum + verbConf_cum +  matlConf_cum +
          ratio_GDP + delta_ratio_GDP + 
          idealpointdistance + delta_idealpointdistance +
          miliexp_ratio +  delta_miliexp_ratio +  milperson_ratio + delta_milperson_ratio +
          sfi_high+ sfi_low + 
          high_GDPPC_log + llow_GDPPC_log +  ccode1_degree+ccode2_degree+
          regime_dur_max + regime_dur_min +
          low_democracy + high_democracy + delta_low_democracy + delta_high_democracy + 
          lnminidist,
  data =data_full, distr = "loglog", silent = TRUE)
save(basket_spdm, file = "Results/basket_spdm.RData")

##############################################################################
#        Subsetting for low polity sample
#        Estimating SPDM model with  low polity sample
##############################################################################
data_lowpolity <- data_full %>% 
  filter(low_democracy < 0) #median

data_lowpolity <- as.data.frame(data_lowpolity)

basket_spdmlowpolity <- spdur(
  duration ~ verbCoop + verbConf +  matlCoop + matlConf +
            verbCoop_cum + matlCoop_cum + verbConf_cum +  matlConf_cum +
            ratio_GDP + delta_ratio_GDP + 
            idealpointdistance + delta_idealpointdistance +
            miliexp_ratio +  delta_miliexp_ratio +  milperson_ratio + delta_milperson_ratio +
            sfi_high+ sfi_low + 
            high_GDPPC_log + llow_GDPPC_log +  
            regime_dur_max + regime_dur_min +
            low_democracy + high_democracy + delta_low_democracy + delta_high_democracy + 
            lnminidist,
  atrisk ~  verbCoop + verbConf +  matlCoop + matlConf +
            verbCoop_cum + matlCoop_cum + verbConf_cum +  matlConf_cum +
            ratio_GDP + delta_ratio_GDP + 
            idealpointdistance + delta_idealpointdistance +
            miliexp_ratio +  delta_miliexp_ratio +  milperson_ratio + delta_milperson_ratio +
            sfi_high+ sfi_low + 
            high_GDPPC_log + llow_GDPPC_log +  ccode1_degree+ccode2_degree+
            regime_dur_max + regime_dur_min +
            low_democracy + high_democracy + delta_low_democracy + delta_high_democracy + 
            lnminidist ,
  data =data_lowpolity, distr = "loglog", silent = TRUE)
save(basket_spdmlowpolity, file = "Results/basket_spdmlowpolity.RData")
##############################################################################


##############################################################################
## Estimating Random Forest Model
##############################################################################
library(randomForest)
set.seed(1014)
memory.limit(1000000)
RF_base <- randomForest(trigercrisis ~ cooperative + conflictual+
                          cooperative_cum + conflictual_cum + 
                          ratio_GDP + delta_ratio_GDP + 
                          trade_depen + delta_trade_depen +
                          idealpointdistance + delta_idealpointdistance +
                          miliexp_ratio +  delta_miliexp_ratio +  milperson_ratio +
                          delta_milperson_ratio +
                          sfi_high+ sfi_low + 
                           num_baad_high + num_baad_low +
                          high_GDPPC_log + llow_GDPPC_log +  ccode1_degree+ccode2_degree+
                          regime_dur_max + regime_dur_min +
                          low_democracy + high_democracy + delta_low_democracy +
                          delta_high_democracy + lnminidist, 
                       data =data_full, importance=TRUE, na.action=na.omit)
save(RF_base, file = "Results/RF_base.RData")
####################################################### 
##end of code
##############################################################################


